import time
import hashlib
import random

class Node:

    NULL = None
    ROOT = "ROOT"
    NORM = "NORM"

    def __init__(self, label, parent=None, child1=None, child2=None, style=None):
        self.child1 = child1
        self.child2 = child2
        self.parent = parent
        self.style = style
        self.label = label
        self.children = []
        self.id = hashlib.sha224(label + str(time.time()) + str(random.randint(-10000, 10000))).hexdigest()

    def addChild(self, node):
        self.children.append(node)

    def removeChild(self, node):
        for item in self.children:
            if item.getID() == node.getID():
                self.children.remove(item)

    def getChildren(self):
        return self.children

    def getID(self):
        return self.id

    def getStyle(self):
        return self.style

    def getLabel(self):
        return self.label

    def setLabel(self, label):
        self.label = label

    def setChild1(self, child1):
        self.child1 = child1

    def getChild1(self):
        return self.child1

    def setChild2(self, child2):
        self.child2 = child2

    def getChild2(self):
        return self.child2

    def setParent(self, parent):
        self.parent = parent

    def getParent(self):
        return self.parent

    def rotation(self):
        parent = self.parent
        child1 = self.child1
        child2 = self.child2
        if parent.getChild1() == self:
            # Left rotation
            """
            print "Left"
            print "Parent " + parent.getLabel()
            print "Self " + self.getLabel()
            print parent.getLabel()
            print "Parent Parent: " + parent.getParent().getLabel()
            """
            self.parent = parent.getParent()
            if parent.getParent() is not None:
                if parent.getParent().getStyle() == Node.ROOT:
                    parent.getParent().setChild1(self)
                else:
                    if parent.getParent().getChild1() == parent:
                        parent.getParent().setChild1(self)
                    elif parent.getParent().getChild2() == parent:
                        parent.getParent().setChild2(self)
            self.child2 = parent
            parent.setChild1(child2)
            parent.setParent(self)
        elif parent.getChild2() == self:
            # Right Rotation
            """
            print "Right"
            print "Parent" + parent.getLabel()
            print "Self " + self.getLabel()
            print parent.getLabel()
            print "Parent Parent: " + parent.getParent().getLabel()
            """
            self.parent = parent.getParent()
            if parent.getParent().getStyle() == Node.ROOT:
                parent.getParent().setChild1(self)
            else:
                if parent.getParent().getChild1() == parent:
                    parent.getParent().setChild1(self)
                elif parent.getParent().getChild2() == parent:
                    parent.getParent().setChild2(self)
            self.child1 = parent
            parent.setChild2(child1)
            parent.setParent(self)

    
        
